{include file="header.htm" /}
<script type="text/javascript" src="__PUBLIC__/plugins/colpick/js/colpick.js"></script>
<link href="__PUBLIC__/plugins/colpick/css/colpick.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
    #coupon_color {
        /*margin:0;*/
        /*padding:0;*/
        border:solid 1px #dc143c;
        width:70px;
        height:20px;
        border-right:40px solid green;
        /*line-height:20px;*/
    }
</style>

<body class="bodystyle" style="overflow-y: scroll; cursor: default; -moz-user-select: inherit;">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    <div class="fixed-bar">
        <div class="item-title">
            <a class="back_xin" href="{:weapp_url('Coupons/Coupons/index')}" title="返回"><i class="iconfont e-fanhui"></i></a>
            <div class="subject">
                <h3>编辑优惠券</h3>
                <h5></h5>
            </div>
        </div>
    </div><strong></strong>
    <form class="form-horizontal" id="postForm" action="{:weapp_url('Coupons/Coupons/edit')}" method="post">
        <input type="hidden" name="coupon_id" value="{$info.coupon_id}">
        <div class="ncap-form-default">
            <dl class="row">
                <dt class="tit">
                    <label for="coupon_name"><em>*</em>优惠券名称</label>
                </dt>
                <dd class="opt">
                    <input type="text" name="coupon_name" id="coupon_name" value="{$info.coupon_name}" class="input-txt" autocomplete="off">
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="coupon_form"><em>*</em>优惠券类型</label>
                </dt>
                <dd class="opt">
                    <label><input type="radio" name="coupon_form" value="1" {eq name="$info.coupon_form" value="1"} checked="checked" {/eq}/>&nbsp;满减券</label>
                    &nbsp; &nbsp;
                    <label><input type="radio" name="coupon_form" value="2" {eq name="$info.coupon_form" value="2"} checked="checked" {/eq}/>&nbsp;折扣券</label>
                    <p class="notic">消费金额填写0元即为无门槛券</p>
                    <div class="coupons_poplistdiv mt10">
                        满
                        <input type="text" autocomplete="off" name="conditions_use" id="conditions_use" value="{$info.conditions_use}" class="input-txt" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" style="width: 80px !important;">
                        元，<span id="coupon_form_span1" {eq name="$info.coupon_form" value="2"} class="none" {/eq}>减
                        <input type="text" autocomplete="off" name="coupon_price" id="coupon_price" value="{$info.coupon_price}" class="input-txt" onpaste="this.value=this.value.replace(/[^\d]/g, '');" onkeyup="this.value=this.value.replace(/[^\d]/g, '');" style="width: 80px !important;">
                        元</span><span id="coupon_form_span2" {eq name="$info.coupon_form" value="1"} class="none" {/eq}>享
                        <input type="text" autocomplete="off" name="coupon_discount" id="coupon_discount" value="{$info.coupon_discount}" class="input-txt" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" style="width: 80px !important;">
                        折</span>
                    </div>
                </dd>
            </dl>

            <dl class="row">
                <dt class="tit">
                    <label for="coupon_stock"><em>*</em>库存</label>
                </dt>
                <dd class="opt">
                    <input type="text" autocomplete="off" name="coupon_stock" id="coupon_stock" value="{$info.coupon_stock}" class="input-txt" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" style="width: 100px !important;">&nbsp;张
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="coupon_type"><em>*</em>可使用商品</label>
                </dt>
                <dd class="opt">
                    <label><input type="radio" name="coupon_type" value="1" {eq name="$info.coupon_type" value="1"} checked="checked" {/eq}/>&nbsp;全站通用</label>
                    &nbsp; &nbsp;
                    <label><input type="radio" name="coupon_type" value="2" {eq name="$info.coupon_type" value="2"} checked="checked" {/eq}/>&nbsp;指定商品</label>
                    &nbsp; &nbsp;
                    <label><input type="radio" name="coupon_type" value="3" {eq name="$info.coupon_type" value="3"} checked="checked" {/eq}/>&nbsp;指定分类</label>
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row {neq name='$info.coupon_type' value='2'} none {/neq}" id="cate2">
                <dt class="tit">
                    <label for="product_id"><em>*</em>商品集合</label>
                </dt>
                <dd class="opt">
                    <a href="JavaScript:void(0);" onclick="Product();" class="ncap-btn ncap-btn-green">选择商品</a>
                    <input type="hidden" name="product_id" id="product_id" value="{$info.product_id}">
                    <input type="hidden" id="SelectProductID" value="{$info.product_id}">
                    <span style="color: red;">已选 <a href="JavaScript:void(0);" id="product_id_num">{$info.product_id_num}</a> 商品</span>
                </dd>
            </dl>
            <script type="text/javascript">
                $( function() {
                    $('#product_id_num').click( function() {
                        var product_ids = $('#product_id').val();
                        var url = "{:weapp_url('Coupons/Coupons/select_product')}";
                        url = url + '&product_ids=' + product_ids;
                        //iframe窗
                        layer.open({
                            type: 2,
                            title: '已选商品列表',
                            shadeClose: false,
                            shade: layer_shade,
                            closeBtn: 3,
                            area: ['80%', '90%'],
                            content: url
                        });
                    });
                });

                function Product() {
                    var url = "{:weapp_url('Coupons/Coupons/select_product')}";
                    //iframe窗
                    layer.open({
                        type: 2,
                        title: '请选择商品',
                        shadeClose: false,
                        shade: layer_shade,
                        closeBtn: 3,
                        area: ['80%', '90%'],
                        content: url
                    });
                }
            </script>

            <dl class="row {neq name='$info.coupon_type' value='3'} none {/neq}" id="cate3">
                <dt class="tit">
                    <label for="arctype_id"><em>*</em>商品分类</label>
                </dt>
                <dd class="opt">
                    <a href="JavaScript:void(0);" onclick="Arctype();" class="ncap-btn ncap-btn-green">选择分类</a>
                    <input type="hidden" name="arctype_id" id="arctype_id" value="{$info.arctype_id}">
                    <input type="hidden" id="SelectArctypeID" value="{$info.arctype_id}">
                    <span style="color: red;">已选 <font id="arctype_id_num">{$info.arctype_id_num}</font> 分类</span>
                </dd>
            </dl>
            <script type="text/javascript">
                //选择分类
                function Arctype()
                {
                    var arctype_ids = $('#arctype_id').val();
                    var url = "{:weapp_url('Coupons/Coupons/select_arctype')}";
                    url = url + '&typeids=' + arctype_ids;

                    var iframes = layer.open({
                        type: 2,
                        title: '请选择分类',
                        fixed: true, //不固定
                        shadeClose: false,
                        shade: layer_shade,
                        // maxmin: true, //开启最大化最小化按钮
                        area: ['80%', '90%'],
                        btn: ['确定', '关闭'],
                        content: url,
                        yes: function(index, layero) {
                            var body = layer.getChildFrame('body', index);
                            var SelectArctypeIDNew = body.find('#post_typeids').val();
                            layer.close(index);
                            $('#arctype_id').val(SelectArctypeIDNew);
                            $('#SelectArctypeID').val(SelectArctypeIDNew);
                            if (SelectArctypeIDNew) {
                                $('#arctype_id_num').html(SelectArctypeIDNew.split(",").length).parent().show();
                            } else {
                                $('#arctype_id_num').parent().hide();
                            }
                        }
                    });
                }
            </script>
            <dl class="row">
                <dt class="tit">
                    <label for="coupon_intro">优惠券说明</label>
                </dt>
                <dd class="opt">
                    <textarea rows="5" cols="60" name="coupon_intro" id="coupon_intro" style="height:60px;" class="ui-input">{$info.coupon_intro}</textarea>
                    <p class="notic"></p>
                </dd>
            </dl>

            <dl class="row">
                <dt class="tit">
                    <label for="level_form"><em>*</em>领取人限制</label>
                </dt>
                <dd class="opt">
                    <label><input type="radio" name="level_form" value="1" {empty name="$info.redeem_authority"} checked="checked" {/empty}/>&nbsp;全部会员</label>
                    &nbsp; &nbsp;
                    <label><input type="radio" name="level_form" value="2" {notempty name="$info.redeem_authority"} checked="checked" {/notempty}/>&nbsp;指定会员身份</label>
                    <p class="notic"></p>
                    <div class="coupons_poplistdiv mt10 {empty name='$info.redeem_authority'} none {/empty}" id="levelform2">
                        {volist name="users_level" id="vo"}
                        <label><input type="checkbox" name="level_id[]" value="{$vo.level_id}"  {in name="$vo.level_id" value="$info.redeem_authority"} checked="checked" {/in}>{$vo.level_name}</label>&nbsp;&nbsp;
                        {/volist}
                    </div>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="level_number"><em>*</em>每人领取张数</label>
                </dt>
                <dd class="opt">
                    <label><input type="radio" name="use_limit_num" value="0" {empty name="$info.use_limit_num"} checked="checked" {/empty} onclick="$('#use_limit_num_div').hide();"/>&nbsp;不限制</label>
                    &nbsp; &nbsp;
                    <label><input type="radio" name="use_limit_num" value="1" {notempty name="$info.use_limit_num"} checked="checked" {/notempty} onclick="$('#use_limit_num_div').show();"/>&nbsp;自定义</label>
                    <p class="notic"></p>
                    <div class="coupons_poplistdiv mt10 {empty name='$info.use_limit_num'} none {/empty}" id="use_limit_num_div">
                        每人限领&nbsp;<input type="text" autocomplete="off" name="use_limit_num_value" value="{$info.use_limit_num}" class="input-txt" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" style="width: 100px !important;">&nbsp;张
                    </div>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label><em>*</em>优惠使用限制</label>
                </dt>
                <dd class="opt">
                    <label><input type="radio" name="use_limit" value="0" {eq name="$info.use_limit" value="0"} checked="checked" {/eq}/>&nbsp;不限制</label>
                    &nbsp; &nbsp;
                    <label><input type="radio" name="use_limit" value="1" {eq name="$info.use_limit" value="1"} checked="checked" {/eq}/>&nbsp;不可与会员折扣同时使用</label>
                    <p class="notic"></p>
                </dd>
            </dl>

            <dl class="row">
                <dt class="tit">
                    <label for="use_type"><em>*</em>使用期限</label>
                </dt>
                <dd class="opt">
                    <label><input type="radio" name="use_type" value="1" {eq name="$info.use_type" value="1"} checked="checked" {/eq} onclick="chooseUseType(this);" />&nbsp;固定时间内有效</label>
                    <input type="text" autocomplete="off" name="use_start_time" id="use_start_time" {eq name="$info.use_type" value="1"} value="{$info.use_start_time|MyDate='Y-m-d H:i:s',###}" {/eq} {neq name="$info.use_type" value="1"} disabled="disabled" {/neq} class="input-txt" style="width: 130px !important;" /> -
                    <input type="text" autocomplete="off" name="use_end_time" id="use_end_time" {eq name="$info.use_type" value="1"} value="{$info.use_end_time|MyDate='Y-m-d H:i:s',###}" {/eq}  {neq name="$info.use_type" value="1"} disabled="disabled" {/neq} class="input-txt" style="width: 130px !important;" />
                    <div class="mt5"></div>
                    <label><input type="radio" name="use_type" value="2" {eq name="$info.use_type" value="2"} checked="checked" {/eq} onclick="chooseUseType(this);" />&nbsp;领到当日开始</label>
                    <input type="text" id="valid_days_2" value="{$info.valid_days}" {neq name="$info.use_type" value="2"} disabled="disabled" {/neq} class="input-txt" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" style="width: 50px !important;">&nbsp;天内有效
                    <div class="mt5"></div>
                    <label><input type="radio" name="use_type" value="3" {eq name="$info.use_type" value="3"} checked="checked" {/eq} onclick="chooseUseType(this);" />&nbsp;领到次日开始</label>
                    <input type="text" id="valid_days_3" value="{$info.valid_days}" {neq name="$info.use_type" value="3"} disabled="disabled" {/neq}  class="input-txt" onpaste="this.value=this.value.replace(/[^\d.]/g, '');" onkeyup="this.value=this.value.replace(/[^\d.]/g, '');" style="width: 50px !important;">&nbsp;天内有效
                    <p class="notic"></p>
                </dd>
            </dl>

            <dl class="row">
                <dt class="tit">
                    <label for="start_date"><em>*</em>发放日期</label>
                </dt>
                <dd class="opt">
                    <input type="text" autocomplete="off" name="start_date" id="start_date" value="{$info.start_date|MyDate='Y-m-d H:i:s',###}" class="input-txt">
                    <p class="notic">优惠券开始发放时间</p>
                </dd>
            </dl>

            <dl class="row">
                <dt class="tit">
                    <label for="end_date"><em>*</em>结束日期</label>
                </dt>
                <dd class="opt">
                    <input type="text" autocomplete="off" name="end_date" id="end_date" value="{$info.end_date|MyDate='Y-m-d H:i:s',###}" class="input-txt">
                    <p class="notic">优惠券发放结束时间</p>
                </dd>
            </dl>

            <div class="bot">
                <input type="hidden" name="valid_days" id="valid_days" value="{$info.valid_days}">

                <a href="JavaScript:void(0);" onclick="checkForm();" class="ncap-btn-big ncap-btn-green" id="submitBtn">保存</a>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    $(document).ready(function(){
        // 颜色选择
        $('#coupon_color').colpick({
            flat:false,
            layout:'rgbhex',
            submit:0,
            colorScheme:'light',
            color:$('#coupon_color').val(),
            onChange:function(hsb,hex,rgb,el,bySetColor) {
                $(el).css('border-color','#'+hex);
                // Fill the text box just if the color was set using the picker, and not the colpickSetColor function.
                if(!bySetColor) $(el).val('#'+hex);
            }
        }).keyup(function(){
            $(this).colpickSetColor('#'+this.value);
        });
    });

    function chooseUseType(obj){
        var val = $(obj).val();
        if ( 1 == val ){
            $("#use_start_time").attr('disabled',false);
            $("#use_end_time").attr('disabled',false);
            $("#valid_days_2").attr('disabled',true);
            $("#valid_days_3").attr('disabled',true);
        }else if( 2 == val ){
            $("#valid_days_2").attr('disabled',false);
            $("#use_start_time").attr('disabled',true);
            $("#use_end_time").attr('disabled',true);
            $("#valid_days_3").attr('disabled',true);
        }else if( 3 == val ){
            $("#valid_days_3").attr('disabled',false);
            $("#use_start_time").attr('disabled',true);
            $("#use_end_time").attr('disabled',true);
            $("#valid_days_2").attr('disabled',true);
        }
    }
    layui.use('laydate', function() {
        var laydate = layui.laydate;

        laydate.render({
            elem: '#start_date'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#end_date'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#use_start_time'
            ,type: 'datetime'
        });
        laydate.render({
            elem: '#use_end_time'
            ,type: 'datetime'
        });
    })
    $(function () {
        $('input[name=coupon_type]').click(function() {
            if (2 == $(this).val()) {
                $('#cate2').show();
            } else {
                $('#cate2').hide();
            }
            if (3 == $(this).val()) {
                $('#cate3').show();
            } else {
                $('#cate3').hide();
            }
        });

        $('input[name=coupon_form]').click(function() {
            if (1 == $(this).val()) {
                $('#coupon_form_span1').show();
                $('#coupon_form_span2').hide();
            } else {
                $('#coupon_form_span1').hide();
                $('#coupon_form_span2').show();
            }
        });

        $('input[name=level_form]').click(function() {
            if (2 == $(this).val()) {
                $('#levelform2').show();
            } else {
                $('#levelform2').hide();
            }
        });
    });

    var parentObj = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

    function checkForm(){
        if ($('#coupon_name').val() == '') {
            showErrorMsg('请填写优惠券名称');
            $('#coupon_name').focus();
            return false;
        }

        if (1 == $("input[name=coupon_form]:checked").val()) {
            var conditions_use = $('#conditions_use').val();
            var coupon_price = $('#coupon_price').val();
            if (conditions_use == '') {
                showErrorMsg('请填写优惠规则-使用条件');
                $('#conditions_use').focus();
                return false;
            }
            if (coupon_price == '') {
                showErrorMsg('请填写优惠规则-减免金额');
                $('#coupon_price').focus();
                return false;
            }
            if (parseInt(conditions_use) < parseInt(coupon_price)) {
                showErrorMsg('优惠规则-使用条件必须大于减免金额');
                $('#conditions_use').focus();
                return false;
            }
        }else{
            var coupon_discount = $('#coupon_discount').val();
            if (coupon_discount == '' || 0 == coupon_discount) {
                showErrorMsg('请填写优惠规则-金额折扣');
                $('#coupon_discount').focus();
                return false;
            }
            if (parseFloat(coupon_discount) < 0.1 || 9.9 < parseFloat(coupon_discount)) {
                showErrorMsg('金额折扣的范围为0.1-9.9');
                $('#coupon_discount').focus();
                return false;
            }
        }
        if (2 == $("input[name=level_form]:checked").val()){
            var level_id_checked = 0;

            $("input[name='level_id[]']:checked").each(function () {
                level_id_checked = $(this).val();
            })
            if (0 == level_id_checked){
                showErrorMsg('请选择会员限制');
                $("input[name='level_id']").focus();
                return false;
            }
        }

        if ($('#coupon_stock').val() == '') {
            showErrorMsg('请填写库存');
            $('#coupon_stock').focus();
            return false;
        }
        var use_type = $("input[name='use_type']:checked").val();

        if ( 1 == use_type ) {
            if ($('#use_start_time').val() == '') {
                showErrorMsg('请填写优惠券有效开始时间');
                $('#use_start_time').focus();
                return false;
            }
            if ($('#use_end_time').val() == '') {
                showErrorMsg('请填写优惠券有效结束时间');
                $('#use_end_time').focus();
                return false;
            }
        }else if ( 2 == use_type ) {
            var valid_days_2 = $('#valid_days_2').val();
            if ( valid_days_2 == '') {
                showErrorMsg('请填写优惠券有效天数');
                $('#valid_days_2').focus();
                return false;
            }
            $("#valid_days").val(valid_days_2)
        }else if ( 3 == use_type ) {
            var valid_days_3 = $('#valid_days_3').val();
            if ( valid_days_3 == '') {
                showErrorMsg('请填写优惠券有效天数');
                $('#valid_days_3').focus();
                return false;
            }
            $("#valid_days").val(valid_days_3)
        }

        if ($('#start_date').val() == '') {
            showErrorMsg('请选择优惠券发放日期');
            $('#start_date').focus();
            return false;
        }

        if ($('#end_date').val() == '') {
            showErrorMsg('请选择优惠券结束日期');
            $('#end_date').focus();
            return false;
        }
        layer_loading('正在处理');
        $.ajax({
            type : 'post',
            url : "{:weapp_url('Coupons/edit', ['_ajax'=>1])}",
            data : $('#postForm').serialize(),
            dataType : 'json',
            success : function(res){
                layer.closeAll();
                if(res.code == 1){
                    layer.msg(res.msg, {icon: 1, shade: layer_shade, time: 1000}, function(){
                        window.location.href = res.url;
                    });
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                layer.closeAll();
                showErrorAlert(e.responseText);
            }
        });
    }
</script>
{include file="footer.htm" /}